export default {
  productList: state => {
    return state.product_List.map(item => {
      return {
        id: item.id,
        name: item.name,
        price: item.price.toFixed(2)
      }
    })
  },

  // added中的id去商品列表中寻找到对应的商品，将信息合并后返回一个新的数组
  cartProductions: state => {
    return state.added.map((prod) => {
      let product = state.product_List.find(item => item.id == prod.id)
      return {
        id: product.id,
        name: product.name,
        price: product.price,
        num: prod.num,
        totalPrice: (product.price * prod.num).toFixed(2)
      }
    })
  },
  // 购物车中商品的总数量
  total_num: (state, getters) => {
    return getters.cartProductions.reduce((total, item) => {
      return total + item.num
    }, 0)
  },
  totalPrice: (state, getters) => {
    return getters.cartProductions.reduce((total, item) => {
      return total + item.price * item.num
    }, 0)
  }
}
